﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：AdvanceSearch.ascx.cs
//
//文件功能描述：对商品进行高级搜索
// 
//创建标识：2007.11.22 何卫红
//修改标识：2007.12.04 何卫红
//创建描述：
//
//------------------------------------*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class AdvanceSearch : System.Web.UI.UserControl
{  

    //实例化WebService对象
    localhost.ProductService product = new localhost.ProductService();

    // 声明高级搜索结果数据集
    DataSet dsAdvanceSearch = new DataSet();

    //定义页的大小
    private static int iPageSize = 8;

    private static int iPageCount;  //总的页数

    private int iRecords;    //总的记录数 

    //初始化各查询字段
    private static string key = string.Empty;
    private static string keyType = string.Empty;
    private static string productType = string.Empty;       
    private static decimal lowerPrice = 0.0M;
    private static decimal highPrice = 0.0M;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Panel1.Visible = true;
            this.pnlAdvanceSearch.Visible = false;
            BindCategory();           
        }
    }

    //在商品类别的 DropDownList中绑定大类别
    protected void BindCategory()
    {
        //异步调用 WebService中获取大类别的方法 ...
        product.GetUpperCategoryListCompleted+=new localhost.GetUpperCategoryListCompletedEventHandler(product_GetUpperCategoryListCompleted);
        product.GetUpperCategoryListAsync();
    }

    //异步回调方法 
    void product_GetUpperCategoryListCompleted(object sender, localhost.GetUpperCategoryListCompletedEventArgs e)
    {
        try
        {
            //异步调用取结果进行数据绑定 
            this.dropProductType.DataSource  = e.Result;
            this.dropProductType.DataTextField = "Name";
            this.dropProductType.DataValueField = "UpperCategoryID";
            this.dropProductType.DataBind();

            ListItem itemProduct = new ListItem("全部商品", "");
            this.dropProductType.Items.Insert(0, itemProduct);
       
        }
        catch(Exception ex)
        {
            Response.Redirect("Msg.aspx?msg=" + ex.Message);
        }
    }

    protected void BindDataList(int iPageIndex)
    {
        lblPage.Text = iPageIndex.ToString();
       
        //异步调用高级搜索方法
        product.AdvanceSeracherProductListCompleted += new localhost.AdvanceSeracherProductListCompletedEventHandler(product_AdvanceSeracherProductListCompleted);
        //异步调用WebService中搜索商品的方法
        product.AdvanceSeracherProductListAsync(iPageSize, iPageIndex, keyType, productType,key,lowerPrice, highPrice);
       
    }

    void product_AdvanceSeracherProductListCompleted(object sender, localhost.AdvanceSeracherProductListCompletedEventArgs e)
    {

        if (e.Result != null)
        {
            //得到返回的结果数据集
            dsAdvanceSearch = e.Result;

            //进行分页处理时将总共的的页号绑定到DropDownList控件中

           
            this.dlstResult.DataSource = dsAdvanceSearch.Tables[0].DefaultView;
            this.dlstResult.DataBind();

            //分页按钮的可用性设置
            ShowButtonPaging();
        }
    }


    /// <summary>
    /// 绑定用来选择页导航的下拉列表
    /// </summary>
    protected void BindDropdownListPageIndex()
    {

        //每次绑定前先进行清空操作
        ddlPage.Items.Clear();     
        ListItem lt;

        for (int i = 0; i < iPageCount; i++)
        {
            lt = new ListItem();
            lt.Text = Convert.ToString(i + 1);
            lt.Value = i.ToString();
            ddlPage.Items.Add(lt);
        }
    }



    /// <summary>
    /// 设定分页按钮的可用性
    /// </summary>
    protected void ShowButtonPaging()
    {

        linkbtnFirst.Enabled = true;
        linkbtnPre.Enabled = true;
        linkbtnNext.Enabled = true;
        linkbtnLast.Enabled = true;

        if (iPageCount == 0 || iPageCount == 1)
        {
            linkbtnFirst.Enabled = false;
            linkbtnPre.Enabled = false;
            linkbtnNext.Enabled = false;
            linkbtnLast.Enabled = false;
        }

        if (int.Parse(lblPage.Text) <= 1)
        {
            //如果是第一页，则将首页与上一页的按钮设为不可用
            linkbtnFirst.Enabled = false;
            linkbtnPre.Enabled = false;
        }
        else if (int.Parse(lblPage.Text) >= iPageCount)
        {
            //如果是最后一页，则将末页与下一页设为不可用
            linkbtnNext.Enabled = false;
            linkbtnLast.Enabled = false;
        }

        else
        {
            //其他情况全部为可用
            linkbtnFirst.Enabled = true;
            linkbtnPre.Enabled = true;
            linkbtnNext.Enabled = true;
            linkbtnLast.Enabled = true;

        }
    }

    /// <summary>
    /// 获取总的记录条数
    /// </summary>
    protected void GetProductCount()
    {
        iRecords = product.AdvanceSeracherProductCount(keyType, productType, key, lowerPrice, highPrice);

        if (iRecords % 8 == 0)
        {
            //记录数刚好可以整除页大小
            iPageCount = iRecords / 8;
        }
        else
        {
            //记录数不能整除页大小
            iPageCount = Convert.ToInt32(iRecords / 8) + 1;
        }

        lblRecords.Text = iRecords.ToString();
    }   
   
    //改变选择页的事件发生时重新绑定数据
    protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(ddlPage.SelectedValue) + 1);

        int page = Convert.ToInt32(lblPage.Text);
        BindDataList(page);
    }

    //当激发到第一页时发生的事件
    protected void linkbtnFirst_Click(object sender, EventArgs e)
    {
        lblPage.Text = "1";
        BindDataList(1);
    }

    //当激发下一页时发生的事件
    protected void linkbtnNext_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) + 1);
        int page = Convert.ToInt32(lblPage.Text);
        BindDataList(page);
    }

    //激发前页记录时发生的事件
    protected void linkbtnPre_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) - 1);
        int page = Convert.ToInt32(lblPage.Text);
        BindDataList(page);
    }

    //激发最后一页记录时发生的事件
    protected void linkbtnLast_Click(object sender, EventArgs e)
    {
        BindDataList(iPageCount);
    }  

    //高级搜索事件触发
    protected void btnStartSearch_Click(object sender, EventArgs e)
    {
        //过滤掉非法字符 取得参数值
        key = this.txtKey.Text.Trim().Replace("'", string.Empty);
        key = key.Replace("--", string.Empty);
        keyType = this.dropKeyType.SelectedValue.Trim();
        productType = this.dropProductType.SelectedValue.Trim();

        try
        {
            lowerPrice = Convert.ToDecimal(this.txtBeginRange.Text.Trim());
            highPrice = Convert.ToDecimal(this.txtEndRange.Text.Trim());
        }
        catch(System.InvalidCastException ex)
        {
            //数据强制转换时出错
            throw ex;
        }

        
        //获取页的总数
        GetProductCount();

        //绑定页的选择索引
        BindDropdownListPageIndex();

        //绑定数据
        BindDataList(1);

        this.Panel1.Visible = false;
        this.pnlAdvanceSearch.Visible = true;
        
    }

     // 对图片的绑定
    protected void dlstResult_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        string strProductID = ((Label)e.Item.FindControl("lblProductID")).Text;

        ((ImageButton)e.Item.FindControl("imgBtnProducter")).Width = Unit.Parse("110px");
        ((ImageButton)e.Item.FindControl("imgBtnProducter")).Height = Unit.Parse("110px");
        ((ImageButton)e.Item.FindControl("imgBtnProducter")).ImageUrl = "../ImagInfo.aspx?productID=" + strProductID + "&&imageID=";
        ((ImageButton)e.Item.FindControl("imgBtnProducter")).PostBackUrl = "../ProductInfo.aspx?productID=" + strProductID;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        int initalX = 1;
        int initalY = 9999;

        this.txtKey.Text = string.Empty;
        this.txtBeginRange.Text = initalX.ToString() ;           
        this.txtEndRange.Text = initalY.ToString();
        this.dropKeyType.SelectedIndex = 0;
        this.dropProductType.SelectedIndex = 0;
    }
}
